package com.yzh.luckydraw.infrastructure.repository;

import com.yzh.luckydraw.common.Constants;
import com.yzh.luckydraw.domain.rule.model.aggregates.TreeRuleRich;
import com.yzh.luckydraw.domain.rule.model.vo.TreeNodeLineVO;
import com.yzh.luckydraw.domain.rule.model.vo.TreeNodeVO;
import com.yzh.luckydraw.domain.rule.model.vo.TreeRootVO;
import com.yzh.luckydraw.domain.rule.repository.IRuleRepository;
import com.yzh.luckydraw.infrastructure.dao.IRuleTreeDao;
import com.yzh.luckydraw.infrastructure.dao.IRuleTreeNodeDao;
import com.yzh.luckydraw.infrastructure.dao.IRuleTreeNodeLineDao;
import com.yzh.luckydraw.infrastructure.po.RuleTree;
import com.yzh.luckydraw.infrastructure.po.RuleTreeNode;
import com.yzh.luckydraw.infrastructure.po.RuleTreeNodeLine;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class RuleRepository implements IRuleRepository {
    @Resource
    private IRuleTreeDao ruleTreeDao;
    @Resource
    private IRuleTreeNodeDao ruleTreeNodeDao;
    @Resource
    private IRuleTreeNodeLineDao ruleTreeNodeLineDao;


    @Override
    public TreeRuleRich queryTreeRuleRich(Long treeId) {
        //查询规则树信息并写入VO对象
        RuleTree ruleTree = ruleTreeDao.queryRuleTreeByTreeId(treeId);
        TreeRootVO treeRootVO = new TreeRootVO();
        treeRootVO.setTreeId(ruleTree.getId());
        treeRootVO.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
        treeRootVO.setTreeName(treeRootVO.getTreeName());
        //查询规则树节点信息
        Map<Long, TreeNodeVO> treeNodeMap = new HashMap<>();
        List<RuleTreeNode> ruleTreeNodes = ruleTreeNodeDao.queryRuleTreeNodeList(treeId);
        //查询链路信息
        List<RuleTreeNodeLine> ruleTreeNodeLines = ruleTreeNodeLineDao.queryRuleTreeNodeLineListByTreeId(treeId);
        //遍历节点对象写入VO
        for (RuleTreeNode ruleTreeNode : ruleTreeNodes) {
            //节点VO中有链路属性，这里需要获取链路信息
            List<TreeNodeLineVO> treeNodeLineVOList = new ArrayList<>();
            if (ruleTreeNode.getNodeType().equals(Constants.NodeType.STEM)) {
                List<RuleTreeNodeLine> ruleTreeNodeLineList = ruleTreeNodeLines.stream().filter(item -> item.getNodeIdFrom().equals(ruleTreeNode.getId())).collect(Collectors.toList());
                
                for (RuleTreeNodeLine treeNodeLine : ruleTreeNodeLineList) {
                    TreeNodeLineVO treeNodeLineVO = new TreeNodeLineVO();
                    treeNodeLineVO.setNodeIdFrom(treeNodeLine.getNodeIdFrom());
                    treeNodeLineVO.setNodeIdTo(treeNodeLine.getNodeIdTo());
                    treeNodeLineVO.setRuleLimitType(treeNodeLine.getRuleLimitType());
                    treeNodeLineVO.setRuleLimitValue(treeNodeLine.getRuleLimitValue());
                    treeNodeLineVOList.add(treeNodeLineVO);
                }
            }
            TreeNodeVO treeNodeVO = new TreeNodeVO();
            treeNodeVO.setTreeId(ruleTreeNode.getTreeId());
            treeNodeVO.setTreeNodeId(ruleTreeNode.getId());
            treeNodeVO.setNodeType(ruleTreeNode.getNodeType());
            treeNodeVO.setNodeValue(ruleTreeNode.getNodeValue());
            treeNodeVO.setRuleKey(ruleTreeNode.getRuleKey());
            treeNodeVO.setRuleDesc(ruleTreeNode.getRuleDesc());
            treeNodeVO.setTreeNodeLineInfoList(treeNodeLineVOList);
            treeNodeMap.put(ruleTreeNode.getId(), treeNodeVO);
        }

        TreeRuleRich treeRuleRich = new TreeRuleRich();
        treeRuleRich.setTreeRoot(treeRootVO);
        treeRuleRich.setTreeNodeMap(treeNodeMap);
        return treeRuleRich;
    }
}
